package com.credit.service.Impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.credit.common.ServerResponse;
import com.credit.mapper.messageListMapper;
import com.credit.mapper.messageMapper;
import com.credit.mapper.messageTypeMapper;
import com.credit.pojo.message;
import com.credit.pojo.messageList;
import com.credit.pojo.messageType;
import com.credit.service.IMessageSerive;
import com.credit.utlis.Tools;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class MessageServiceImpl implements IMessageSerive{


	@Autowired
	private messageTypeMapper messageTypeMapper;

	@Autowired
	private messageMapper messageMapper;

	@Autowired
	private messageListMapper messageListMapper;


    public ServerResponse deleteMessageType(Integer id) {
		
		return ServerResponse.createBySuccess(messageTypeMapper.deleteMessageType(id));
	}

	public ServerResponse insertMessageType(messageType record) {
		
		return ServerResponse.createBySuccess(messageTypeMapper.insertMessageType(record));
	}

	public ServerResponse selectMessageType(Integer id) {
		
		return ServerResponse.createBySuccess(messageTypeMapper.selectMessageType(id));
	}

	public ServerResponse updateMessageType(messageType record) {
		
		return ServerResponse.createBySuccess(messageTypeMapper.updateMessageType(record));
	}
	
	
	
	public ServerResponse insertMessage(message record,String user) {
		//获取系统当前时间
		String date=Tools.formatDateTime(new Date(), "long");
		record.setTime(date);
		//判断消息是否发布
		if(record.getState()==1) {
            messageList messageList=new messageList();
            messageList.setName(record.getName());
            messageList.setTime(date);
            messageList.setType(record.getTypename());
			String[] strs=user.split(",");
			//给用户发布消息
			for (String string : strs) {
				messageList.setUserid(Integer.parseInt(string));
				messageListMapper.insertMessageList(messageList);
			}
			//修改转态为发布转态
			return ServerResponse.createBySuccess(messageMapper.updateMessage(record));
		}
		//添加未发布消息
		return ServerResponse.createBySuccess(messageMapper.insertMessage(record));
	}

	

	public ServerResponse updateMessage(message record) {
		
		return ServerResponse.createBySuccess(messageMapper.updateMessage(record));
	}

	public ServerResponse selectMessage(message m, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		 
		 List<message> list=messageMapper.selectMessage(m);
		 
		 PageInfo pageInfo = new PageInfo(list);
		
		 return ServerResponse.createBySuccess(pageInfo);
	}

	
	
	
	public ServerResponse updateMessageList(String id) {
		String[] str = id.split(",");
		int sum=0;
		for (String string : str) {
			int count=messageListMapper.updateMessageList(Integer.parseInt(string));
			sum=sum+count;
		}
		
		return ServerResponse.createBySuccess(sum);
	}

	
	
	public ServerResponse selectOneMessageList(Integer id) {
		
		messageList messageList = messageListMapper.selectOneMessageList(id);
		//查看详细状态时修改状态为已读
		if(messageList.getState()==0) {
			messageListMapper.updateMessageList(id);
		}

		return ServerResponse.createBySuccess(messageList);
	}

	
	public ServerResponse selectMessageList(messageList m, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		 
		 List<messageList> list=messageListMapper.selectMessageList(m);
		 
		 PageInfo pageInfo = new PageInfo(list);
		
		 return ServerResponse.createBySuccess(pageInfo);
	}
}
